﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.Security;
using NCC_SOP.app_codes;
using System.Data.SqlClient;
using System.Data;

namespace NCC_SOP.Admin.AdministratorOnly
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                initBind();
            }
        }

        protected void initBind()
        {
            try
            {
                dbConnection db = new dbConnection();
                ManagerRoleID.DataSource = db.getAllRoles();
                ManagerRoleID.DataBind();
            }
            catch (Exception e)
            {
                Session["ErrorMessage"] = e.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        protected void NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            try
            {
                dbConnection db = new dbConnection();
                ManagerRoleID.DataSource = db.getAllRoles();
            }
            catch (Exception e1)
            {
                Session["ErrorMessage"] = e1.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        protected void ManageRoleID_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            Session["RoleName"] = item.OwnerTableView.Items[e.Item.ItemIndex]["Role"].Text;
            Response.Redirect("~/Admin/AdministratorOnly/editRole.aspx");
        }

        protected void ManageRoleID_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                dbConnection db = new dbConnection();
                GridEditableItem item = (GridEditableItem)e.Item;
                db.deleteRole(item.OwnerTableView.Items[e.Item.ItemIndex]["Role"].Text);
            }
            catch (Exception e2)
            {
                Session["ErrorMessage"] = e2.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }
    }
}